// mypage/newsDetail/newsDetail.js
var app=getApp();
var util=require("../../utils/util");
var my=require("../../api/my");

Page({
  /*页面的初始数据*/
  data: {
    type: 1,
    news: [],
    currentPage: 1,
    pageSize: 10,
    hasMoreDate: true,
    token: '',
    payStep: 1
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    that.setData({type: options.type,token:wx.getStorageSync('token')})
    //
    that.read_news(options.type)
    that.get_newsDetail_list(options.type)
  },
  // 跳转
  jump_order:function(){
    wx.redirectTo({
      url: '../myOrder/myOrder',
    })
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,type=that.data.type;
    if(hasMoreData){
      that.get_newsDetail_list(type)
    }else{
      util.page_show_toast('没有更多了',1500)
    }
  },
  // 消息已读
  read_news:function(type){
    var that=this,token=that.data.token;
    var params={"type":type};
    my.read_news(params,token).then(res=>{

    })
  },
  // 分页
  get_newsDetail_list:function(type){
    wx.showLoading({title: '加载中'})
    var that=this,token=that.data.token,currentPage=that.data.currentPage,pageSize=that.data.pageSize;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "type": type
    };
    my.get_newsDetail_list(params,token).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.records;
        for(var i=0;i<list.length;i++){
          list[i].time=list[i].statusTime?util.formatTime(new Date(list[i].statusTime)):util.formatTime(new Date())
        }
        var news = that.data.news;
        news = util.extend(news, list)
        if (list.length < pageSize) {
          that.setData({
            news: news,
            hasMoreData: false
          })
        } else {
          that.setData({
            news: news,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
      }
    })
  },
})